package com.young.shop.api.controller;

import com.young.shop.api.service.CouponService;
import com.young.shop.pojo.entity.Coupon;
import com.young.shop.pojo.entity.Goods;
import com.young.shop.pojo.qo.CouponQO;
import com.young.shop.pojo.vo.BaseRespVO;
import com.young.shop.pojo.vo.MyShopCartsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

@Api(tags = "优惠券业务")
@RestController
@RequestMapping("/coupon")
public class CouponController extends BaseController{

    @Autowired
    private CouponService couponService;

    @ApiOperation("获取商城全部优惠券")
    @GetMapping("/showCoupon")
    public BaseRespVO getCoupon(){

        //调用service
        try {
            List<Coupon> coupon = couponService.getCoupon();
            coupon.forEach(item->{
                System.out.println(item.toString());
            });
            return convertSuccessResult(coupon);
        } catch (RuntimeException e) {
            String errorMsg = e.getMessage();
            return convertErrorResult(errorMsg);
        }
    }


    @ApiOperation("用户领取优惠券")
    @PostMapping("/receiveCoupon")

    public BaseRespVO receiveCoupon(Integer userId, String code){
        System.out.println(code);
        System.out.println(userId);
        //调用service
        try {
            int i = couponService.receiveCoupon(userId, code);
            return convertSuccessResult(i);
        } catch (RuntimeException e) {
            String errorMsg = e.getMessage();
            return convertErrorResult(errorMsg);
        }
    }


    @ApiOperation("用户领取优惠券")
    @PostMapping("/getPersonCoupon")

    public BaseRespVO getPersonCoupon(Integer userId){

        System.out.println(userId);
        //调用service
        try {
            List<CouponQO> personCoupon = couponService.getPersonCoupon(userId);
            return convertSuccessResult(personCoupon);
        } catch (RuntimeException e) {
            String errorMsg = e.getMessage();
            return convertErrorResult(errorMsg);
        }
    }

    @ApiOperation("支付頁面查詢可以使用的優惠券")
    @PostMapping("/payCoupon")

    public BaseRespVO getPayCoupon(BigDecimal orderPrice, Integer userId){
        System.out.println(orderPrice);
        System.out.println(userId);
        //调用service
        try {
            List<CouponQO> payCoupon = couponService.getPayCoupon(orderPrice, userId);
            return convertSuccessResult(payCoupon);
        } catch (RuntimeException e) {
            String errorMsg = e.getMessage();
            return convertErrorResult(errorMsg);
        }
    }


}
